home *** CD-ROM | disk | FTP | other *** search
- Imports System.Diagnostics
-
- Public Class ProcessForm
- Inherits System.Windows.Forms.Form
-
- #Region " Windows Form Designer generated code "
-
- Public Sub New()
- MyBase.New()
-
- 'This call is required by the Windows Form Designer.
- InitializeComponent()
-
- 'Add any initialization after the InitializeComponent() call
-
- End Sub
-
- 'Form overrides dispose to clean up the component list.
- Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
- If disposing Then
- If Not (components Is Nothing) Then
- components.Dispose()
- End If
- End If
- MyBase.Dispose(disposing)
- End Sub
- Friend WithEvents Button1 As System.Windows.Forms.Button
- Friend WithEvents Button2 As System.Windows.Forms.Button
- Friend WithEvents Button3 As System.Windows.Forms.Button
- Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
- Friend WithEvents txtOutput As System.Windows.Forms.TextBox
- Friend WithEvents Button4 As System.Windows.Forms.Button
- Friend WithEvents Button5 As System.Windows.Forms.Button
- Friend WithEvents lstProcesses As System.Windows.Forms.ListBox
- Friend WithEvents Button6 As System.Windows.Forms.Button
- Friend WithEvents Button7 As System.Windows.Forms.Button
- Friend WithEvents lblMessage As System.Windows.Forms.Label
-
- 'Required by the Windows Form Designer
- Private components As System.ComponentModel.Container
-
- 'NOTE: The following procedure is required by the Windows Form Designer
- 'It can be modified using the Windows Form Designer.
- 'Do not modify it using the code editor.
- <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
- Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog()
- Me.Button4 = New System.Windows.Forms.Button()
- Me.Button5 = New System.Windows.Forms.Button()
- Me.Button6 = New System.Windows.Forms.Button()
- Me.Button7 = New System.Windows.Forms.Button()
- Me.lstProcesses = New System.Windows.Forms.ListBox()
- Me.Button1 = New System.Windows.Forms.Button()
- Me.Button2 = New System.Windows.Forms.Button()
- Me.Button3 = New System.Windows.Forms.Button()
- Me.txtOutput = New System.Windows.Forms.TextBox()
- Me.lblMessage = New System.Windows.Forms.Label()
- Me.SuspendLayout()
- '
- 'Button4
- '
- Me.Button4.Location = New System.Drawing.Point(16, 304)
- Me.Button4.Name = "Button4"
- Me.Button4.Size = New System.Drawing.Size(144, 40)
- Me.Button4.TabIndex = 3
- Me.Button4.Text = "Close the process"
- '
- 'Button5
- '
- Me.Button5.Location = New System.Drawing.Point(16, 160)
- Me.Button5.Name = "Button5"
- Me.Button5.Size = New System.Drawing.Size(144, 40)
- Me.Button5.TabIndex = 3
- Me.Button5.Text = "Info on current process"
- '
- 'Button6
- '
- Me.Button6.Location = New System.Drawing.Point(16, 208)
- Me.Button6.Name = "Button6"
- Me.Button6.Size = New System.Drawing.Size(144, 40)
- Me.Button6.TabIndex = 3
- Me.Button6.Text = "Info on running processes"
- '
- 'Button7
- '
- Me.Button7.Location = New System.Drawing.Point(16, 256)
- Me.Button7.Name = "Button7"
- Me.Button7.Size = New System.Drawing.Size(144, 40)
- Me.Button7.TabIndex = 3
- Me.Button7.Text = "Search process"
- '
- 'lstProcesses
- '
- Me.lstProcesses.Anchor = ((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
- Or System.Windows.Forms.AnchorStyles.Right)
- Me.lstProcesses.ItemHeight = 18
- Me.lstProcesses.Location = New System.Drawing.Point(176, 16)
- Me.lstProcesses.Name = "lstProcesses"
- Me.lstProcesses.Size = New System.Drawing.Size(368, 94)
- Me.lstProcesses.Sorted = True
- Me.lstProcesses.TabIndex = 4
- '
- 'Button1
- '
- Me.Button1.Location = New System.Drawing.Point(16, 16)
- Me.Button1.Name = "Button1"
- Me.Button1.Size = New System.Drawing.Size(144, 40)
- Me.Button1.TabIndex = 0
- Me.Button1.Text = "Load Autoexec.bat in Notepad"
- '
- 'Button2
- '
- Me.Button2.Location = New System.Drawing.Point(16, 64)
- Me.Button2.Name = "Button2"
- Me.Button2.Size = New System.Drawing.Size(144, 40)
- Me.Button2.TabIndex = 0
- Me.Button2.Text = "Load a document"
- '
- 'Button3
- '
- Me.Button3.Location = New System.Drawing.Point(16, 112)
- Me.Button3.Name = "Button3"
- Me.Button3.Size = New System.Drawing.Size(144, 40)
- Me.Button3.TabIndex = 1
- Me.Button3.Text = "Redirect Find output"
- '
- 'txtOutput
- '
- Me.txtOutput.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
- Or System.Windows.Forms.AnchorStyles.Left) _
- Or System.Windows.Forms.AnchorStyles.Right)
- Me.txtOutput.Location = New System.Drawing.Point(176, 128)
- Me.txtOutput.Multiline = True
- Me.txtOutput.Name = "txtOutput"
- Me.txtOutput.ScrollBars = System.Windows.Forms.ScrollBars.Both
- Me.txtOutput.Size = New System.Drawing.Size(368, 184)
- Me.txtOutput.TabIndex = 2
- Me.txtOutput.Text = ""
- Me.txtOutput.WordWrap = False
- '
- 'lblMessage
- '
- Me.lblMessage.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
- Or System.Windows.Forms.AnchorStyles.Right)
- Me.lblMessage.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
- Me.lblMessage.Location = New System.Drawing.Point(176, 320)
- Me.lblMessage.Name = "lblMessage"
- Me.lblMessage.Size = New System.Drawing.Size(368, 24)
- Me.lblMessage.TabIndex = 6
- '
- 'ProcessForm
- '
- Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
- Me.ClientSize = New System.Drawing.Size(552, 349)
- Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblMessage, Me.Button7, Me.Button6, Me.lstProcesses, Me.Button5, Me.Button4, Me.txtOutput, Me.Button3, Me.Button2, Me.Button1})
- Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
- Me.Name = "ProcessForm"
- Me.Text = "Processes"
- Me.ResumeLayout(False)
-
- End Sub
-
- #End Region
-
- ' run Notepad and list environment variables
-
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- Dim proc As New Process()
- ' Prepare to run Notepad and load C:\Autoexec.it in it.
- proc.StartInfo.FileName = "Notepad.exe"
- ' get the name of the C:\WINDOWS directory
- proc.StartInfo.WorkingDirectory = IO.Path.GetDirectoryName(Environment.SystemDirectory)
- proc.StartInfo.Arguments = "c:\autoexec.bat"
-
- ' List all environment variables.
- Dim de As DictionaryEntry
- For Each de In proc.StartInfo.EnvironmentVariables
- Debug.WriteLine(de.Key.ToString & "=" & de.Value.ToString)
- Next
-
- ' ensure that error dialogs are displayed correctly.
- proc.StartInfo.ErrorDialog = True
- proc.StartInfo.ErrorDialogParentHandle = Me.Handle
- proc.StartInfo.UseShellExecute = False
-
- ' run it.
- proc.Start()
- End Sub
-
- ' run an executable associated with a file extension
-
- Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
- Dim proc As New Process()
-
- OpenFileDialog1.CheckFileExists = True
- OpenFileDialog1.Filter = "All Files|*.*"
- If OpenFileDialog1.ShowDialog = DialogResult.OK Then
-
- ' Prepare to run Notepad and load C:\Autoexec.it in it.
- proc.StartInfo.FileName = OpenFileDialog1.FileName
- proc.StartInfo.ErrorDialog = True
- proc.StartInfo.ErrorDialogParentHandle = Me.Handle
-
- Try
- proc.Start()
- Catch ex As Exception
- MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
- Exit Sub
- End Try
- End If
-
- #Const WAIT_UNTIL_PROCESS_ENDS = False
-
- #If WAIT_UNTIL_PROCESS_ENDS Then
- proc.WaitForExit()
- lblMessage.Text = "Application has exited - Exit code = " & proc.ExitCode.ToString
- Beep()
- #End If
-
- End Sub
-
- ' demonstrates how you can call a command-line program
- ' and process its output
-
- Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
- Dim proc As New Process()
-
- ' Search all the lines in C:\AUTOEXEC.BAT that contain a "x" character.
- proc.StartInfo.FileName = "find ""x"""
- proc.StartInfo.Arguments = "c:\autoexec.bat"
- ' UseShellExecute must be False.
- proc.StartInfo.UseShellExecute = False
- ' Redirect the standard output channel.
- proc.StartInfo.RedirectStandardOutput = True
- ' Suppress the creation of the console window.
- proc.StartInfo.CreateNoWindow = True
- proc.Start()
-
- ' Get the StreamReader that points to output data.
- Dim sr As System.IO.StreamReader = proc.StandardOutput
- ' Display the output data in a TextBox control.
- txtOutput.Text = sr.ReadToEnd
- ' Close the stream.
- sr.Close()
- End Sub
-
- ' check whether a process is responding, and kill it
-
- Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
- ' Exit if no selected process.
- If lstProcesses.SelectedItem Is Nothing Then Exit Sub
-
- ' Get a reference to the highlighted process.
- Dim proc As Process = CType(lstProcesses.SelectedItem, Process)
- Try
- If proc.Responding Then
- proc.CloseMainWindow()
- Else
- proc.Kill()
- End If
- Catch ex As Exception
- MessageBox.Show(ex.Message, "Unable to Kill")
- End Try
- End Sub
-
- ' Display information on current process.
-
- Private Sub Button5_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button5.Click
- txtOutput.Text = GetProcessInfo(Process.GetCurrentProcess)
- End Sub
-
- ' load info on all running processes
-
- Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
- Dim p As Process
- ' This is necessary to cope with Processes that don't allow to trap events from.
- On Error Resume Next
- ' unbind processes from event handlers.
- For Each p In lstProcesses.Items
- RemoveHandler p.Exited, AddressOf Process_Exited
- Next
- ' Clear listbox elements.
- lstProcesses.Items.Clear()
-
- ' Display ProcessName property in the list area.
- lstProcesses.DisplayMember = "ProcessName"
-
- ' Load info on all running process in the ListBox control.
- For Each p In Process.GetProcesses
- lstProcesses.Items.Add(p)
- ' Bind each element to the event handler.
- AddHandler p.Exited, AddressOf Process_Exited
- p.EnableRaisingEvents = True
- Next
-
- ' display info on first process
- lstProcesses.SelectedIndex = 0
- End Sub
-
- ' get array of all processes with a given name
-
- Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
- ' ask for a process name - defaults to Notepad
- Dim procName As String
- procName = InputBox("Enter the name of a process", "Search Process", "notepad")
- If procName = "" Then Exit Sub
-
- ' get an array with all processes with that name.
- Dim procs() As Process = Process.GetProcessesByName(procName)
- If procs.Length = 0 Then
- MessageBox.Show("Process not found", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
- Exit Sub
- End If
-
- ' This is necessary to cope with Processes that don't allow to trap events from.
- On Error Resume Next
-
- Dim p As Process
- ' unbind processes from event handlers.
- For Each p In lstProcesses.Items
- RemoveHandler p.Exited, AddressOf Process_Exited
- Next
- ' Clear listbox elements.
- lstProcesses.Items.Clear()
-
- ' Display ProcessName property in the list area.
- lstProcesses.DisplayMember = "ProcessName"
-
- ' Load info on matching processes in the ListBox control.
- For Each p In procs
- lstProcesses.Items.Add(p)
- AddHandler p.Exited, AddressOf Process_Exited
- p.EnableRaisingEvents = True
- Next
- ' display info on first process
- lstProcesses.SelectedIndex = 0
- End Sub
-
- ' This routine is invoked when one of the watched process terminates
-
- Private Sub Process_Exited(ByVal sender As Object, ByVal e As EventArgs)
- ' Get a reference to the process in question.
- Dim proc As Process = CType(sender, Process)
- lblMessage.Text = String.Format("Process {0} has exited - Exit code is {1}", proc.Id, proc.ExitCode)
- Beep()
- End Sub
-
- ' Display information on selected process.
-
- Private Sub lstProcesses_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstProcesses.SelectedIndexChanged
- ' Get the selected process.
- Dim proc As Process = CType(lstProcesses.SelectedItem, Process)
- ' Display information on this process.
- txtOutput.Text = GetProcessInfo(proc)
- End Sub
-
- ' Return a string describing a process.
- Function GetProcessInfo(ByVal proc As Process) As String
- Dim res As String
- Dim crlf As String = ControlChars.CrLf
- Dim mo As ProcessModule
- Dim th As ProcessThread
-
- ' Ignore properties that raise an error.
- On Error Resume Next
-
- ' Ensure data is up-to-date.
- proc.Refresh()
-
- res &= "=== PROCESS " & proc.ProcessName & crlf
- res &= "ID = " & proc.Id & crlf
- res &= "BasePriority = " & proc.BasePriority.ToString & crlf
- ' Get main window information.
- res &= "MainWindowTitle = " & proc.MainWindowTitle & crlf
- res &= "MainWindowHandle = " & Hex(proc.MainWindowHandle.ToInt32) & crlf
- res &= crlf
-
- ' Get time information.
- res &= "StartTime = " & proc.StartTime.ToString & crlf
- res &= "UserProcessorTime = " & proc.UserProcessorTime.ToString & crlf
- res &= "PrivilegedProcessorTime = " & proc.PrivilegedProcessorTime.ToString & crlf
- res &= "TotalProcessorTime = " & proc.TotalProcessorTime.ToString & crlf
- res &= crlf
-
- ' Get Working set information.
- res &= "WorkingSet = " & proc.WorkingSet.ToString & crlf
- res &= "MinWorkingSet = " & proc.MinWorkingSet.ToString & crlf
- res &= "MaxWorkingSet = " & proc.MaxWorkingSet.ToString & crlf
- res &= "PeakWorkingSet = " & proc.PeakWorkingSet & crlf
- res &= crlf
-
- ' Get memory information.
- res &= "VirtualMemorySize = " & proc.VirtualMemorySize & crlf
- res &= "NonpagedSystemMemorySize = " & proc.NonpagedSystemMemorySize & crlf
- res &= "PagedMemorySize = " & proc.PagedMemorySize & crlf
- res &= "PagedSystemMemorySize = " & proc.PagedSystemMemorySize & crlf
- res &= "PeakPagedMemorySize = " & proc.PeakPagedMemorySize & crlf
- res &= "PeakVirtualMemorySize = " & proc.PeakVirtualMemorySize & crlf
- res &= "PrivateMemorySize = " & proc.PrivateMemorySize & crlf
- res &= crlf
-
-
- ' Get information on all modules
- res &= "MODULES : " & crlf
- res &= "MainModule = " & proc.MainModule.ModuleName & crlf
-
- For Each mo In proc.Modules
- res &= " Module Name = " & mo.ModuleName & crlf
- res &= " FileName = " & mo.FileName & crlf
- res &= " BaseAddress = " & Hex(mo.BaseAddress.ToInt32) & crlf
- res &= " Version = " & mo.FileVersionInfo.FileVersion & crlf
- res &= " Memory size = " & mo.ModuleMemorySize & crlf
- Next
- res &= crlf
-
- ' Get information on all threads.
- res &= "THREADS : " & crlf
- For Each th In proc.Threads
- res &= " Thread ID = " & th.Id.ToString & crlf
- res &= " StartTime = " & th.StartTime.ToString & crlf
- res &= " BasePriority = " & th.BasePriority & crlf
- res &= " PriorityLevel = " & th.PriorityLevel & crlf
- res &= " ThreadState = " & th.ThreadState.ToString & crlf
- res &= " UserProcessorTime = " & th.UserProcessorTime.ToString & crlf
- res &= " TotalProcessorTime = " & th.TotalProcessorTime.ToString & crlf
- Next
- res &= crlf
-
- Return res
- End Function
-
- End Class
-